<template>
  <div class="header-renderer" :class="mode">
    <!-- 设计模式 -->
    <div v-if="mode === 'design'" class="design-mode">
      <div class="header-preview" :class="{ 'with-border': headerConfig.showBorder }">
        <div class="header-content">
          <div class="title-section">
            <h3 class="header-title">{{ headerConfig.title || '页眉标题' }}</h3>
            <p v-if="headerConfig.subtitle" class="header-subtitle">
              {{ headerConfig.subtitle }}
            </p>
          </div>
          
          <div v-if="headerConfig.logo" class="logo-section">
            <img :src="headerConfig.logo" alt="Logo" class="logo-preview" />
          </div>
        </div>
        
        <div v-if="headerConfig.extraInfo" class="extra-info">
          {{ headerConfig.extraInfo }}
        </div>
      </div>
      
      <div class="header-actions">
        <el-button 
          size="mini" 
          @click="editHeader"
          icon="el-icon-edit"
        >
          编辑页眉
        </el-button>
      </div>
    </div>

    <!-- 预览和生成模式 -->
    <div v-else class="preview-mode">
      <div class="header-display" :class="{ 'with-border': headerConfig.showBorder }" :style="headerStyle">
        <div class="header-main">
          <!-- Logo区域 -->
          <div v-if="headerConfig.logo" class="logo-display">
            <img 
              :src="headerConfig.logo" 
              alt="Logo" 
              class="logo-image"
              :style="logoStyle"
            />
          </div>
          
          <!-- 标题区域 -->
          <div class="title-display" :style="titleStyle">
            <h1 class="main-title">{{ processedTitle }}</h1>
            <p v-if="headerConfig.subtitle" class="subtitle">
              {{ processedSubtitle }}
            </p>
          </div>
          
          <!-- 额外信息区域 -->
          <div v-if="headerConfig.extraInfo" class="extra-display">
            <div class="extra-content" v-html="processedExtraInfo"></div>
          </div>
        </div>
        
        <!-- 底部信息 -->
        <div v-if="headerConfig.bottomInfo" class="bottom-info">
          {{ processedBottomInfo }}
        </div>
      </div>
    </div>

    <!-- 页眉编辑对话框 -->
    <el-dialog
      title="编辑页眉"
      :visible.sync="editDialog.visible"
      width="700px"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <header-editor
        v-if="editDialog.visible"
        :header-config="headerConfig"
        @save="handleHeaderSave"
        @cancel="editDialog.visible = false"
      />
    </el-dialog>
  </div>
</template>

<script>
import HeaderEditor from './HeaderEditor.vue'

export default {
  name: 'HeaderRenderer',
  components: {
    HeaderEditor
  },
  props: {
    component: {
      type: Object,
      required: true,
      default: () => ({
        content: {
          title: '检测报告',
          subtitle: '',
          logo: null,
          showBorder: true,
          extraInfo: '',
          bottomInfo: ''
        }
      })
    },
    config: {
      type: Object,
      default: () => ({})
    },
    mode: {
      type: String,
      default: 'design'
    },
    testData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      editDialog: {
        visible: false
      }
    }
  },
  computed: {
    headerConfig() {
      return this.component.content || {}
    },
    
    headerStyle() {
      const styles = {
        padding: '20px',
        backgroundColor: this.headerConfig.backgroundColor || '#ffffff'
      }
      
      if (this.headerConfig.showBorder) {
        styles.borderBottom = '2px solid #409eff'
      }
      
      // 合并组件样式
      if (this.component.style) {
        Object.assign(styles, this.component.style)
      }
      
      return styles
    },
    
    logoStyle() {
      return {
        maxWidth: this.headerConfig.logoWidth || '80px',
        maxHeight: this.headerConfig.logoHeight || '60px'
      }
    },
    
    titleStyle() {
      return {
        textAlign: this.headerConfig.titleAlign || 'center',
        flex: this.headerConfig.logo ? '1' : 'none'
      }
    },
    
    processedTitle() {
      return this.replaceVariables(this.headerConfig.title || '检测报告')
    },
    
    processedSubtitle() {
      return this.replaceVariables(this.headerConfig.subtitle || '')
    },
    
    processedExtraInfo() {
      let info = this.headerConfig.extraInfo || ''
      info = this.replaceVariables(info)
      return info.replace(/\n/g, '<br>')
    },
    
    processedBottomInfo() {
      return this.replaceVariables(this.headerConfig.bottomInfo || '')
    }
  },
  methods: {
    /**
     * 编辑页眉
     */
    editHeader() {
      this.editDialog.visible = true
    },
    
    /**
     * 处理页眉保存
     */
    handleHeaderSave(updatedConfig) {
      this.$emit('update', {
        content: updatedConfig
      })
      this.editDialog.visible = false
    },
    
    /**
     * 替换变量
     */
    replaceVariables(text) {
      if (!text || typeof text !== 'string') return text
      
      return text.replace(/\{\{(\w+)\}\}/g, (match, variable) => {
        const value = this.findVariableValue(variable)
        return value !== undefined ? value : match
      })
    },
    
    /**
     * 查找变量值
     */
    findVariableValue(variable) {
      if (!this.testData) return undefined
      
      // 在基本信息中查找
      if (this.testData.basicInfo && this.testData.basicInfo[variable] !== undefined) {
        return this.testData.basicInfo[variable]
      }
      
      // 特殊变量处理
      const specialVars = {
        currentDate: new Date().toLocaleDateString(),
        currentTime: new Date().toLocaleTimeString(),
        currentYear: new Date().getFullYear().toString()
      }
      
      if (specialVars[variable] !== undefined) {
        return specialVars[variable]
      }
      
      return undefined
    }
  }
}
</script>

<style scoped>
.header-renderer {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

.design-mode {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.header-preview {
  flex: 1;
  border: 1px dashed #dcdfe6;
  border-radius: 4px;
  padding: 16px;
  background: #f8f9fa;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.header-preview.with-border {
  border-bottom: 2px solid #409eff;
  border-style: solid;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.title-section {
  flex: 1;
}

.header-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-subtitle {
  margin: 0;
  font-size: 14px;
  color: #606266;
}

.logo-section {
  flex-shrink: 0;
}

.logo-preview {
  max-width: 60px;
  max-height: 40px;
  object-fit: contain;
}

.extra-info {
  padding-top: 8px;
  border-top: 1px solid #e6e8eb;
  font-size: 12px;
  color: #909399;
}

.header-actions {
  display: flex;
  justify-content: center;
}

.preview-mode {
  width: 100%;
  height: 100%;
}

.header-display {
  width: 100%;
  height: 100%;
  background: white;
}

.header-main {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 12px;
}

.logo-display {
  flex-shrink: 0;
}

.logo-image {
  object-fit: contain;
}

.title-display {
  flex: 1;
}

.main-title {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  line-height: 1.2;
}

.subtitle {
  margin: 0;
  font-size: 16px;
  color: #606266;
  line-height: 1.4;
}

.extra-display {
  flex-shrink: 0;
  text-align: right;
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

.extra-content {
  white-space: pre-line;
}

.bottom-info {
  padding-top: 8px;
  border-top: 1px solid #e6e8eb;
  font-size: 12px;
  color: #909399;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-preview {
    padding: 12px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
  
  .header-main {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
  
  .extra-display {
    text-align: center;
  }
  
  .main-title {
    font-size: 20px;
  }
  
  .subtitle {
    font-size: 14px;
  }
}
</style>